﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using SHomeWorkshop.LunarPresentation.Commands;
using SHomeWorkshop.LunarPresentation.Interfaces;
using WinForm = System.Windows.Forms;

namespace SHomeWorkshop.LunarPresentation
{
    public static class ExContextMenus
    {
        static ExContextMenus()
        {
            #region 箭头式快捷菜单
            arrowContextMenu = new ContextMenu();
            arrowContextMenu.Opened += new RoutedEventHandler(arrowContextMenu_Opened);
            TextOptions.SetTextFormattingMode(arrowContextMenu, TextFormattingMode.Display);

            MenuItem cmiArrowLocation = new MenuItem() { Header = "箭头位置(_L)" };

            cmiArrowStart = new MenuItem() { Header = "首箭头(_S)" };
            cmiArrowStart.Click += new RoutedEventHandler(cmiArrowStart_Click);

            cmiArrowEnd = new MenuItem() { Header = "尾箭头(_E)" };
            cmiArrowEnd.Click += new System.Windows.RoutedEventHandler(arrowExample_Click);

            cmiArrowAll = new MenuItem() { Header = "双箭头(_A)" };
            cmiArrowAll.Click += new RoutedEventHandler(cmiArrowAll_Click);

            cmiArrowNone = new MenuItem() { Header = "无箭头(_N)" };
            cmiArrowNone.Click += new RoutedEventHandler(cmiArrowNone_Click);

            MenuItem arrowheader = BuildMenuHeader("箭头");
            arrowheader.Click += new RoutedEventHandler(arrowheader_Click);
            arrowContextMenu.Items.Add(arrowheader);

            arrowContextMenu.Items.Add(new Separator());

            cmiArrowLocation.Items.Add(cmiArrowStart);
            cmiArrowLocation.Items.Add(cmiArrowEnd);
            cmiArrowLocation.Items.Add(cmiArrowAll);
            cmiArrowLocation.Items.Add(cmiArrowNone);

            arrowContextMenu.Items.Add(cmiArrowLocation);

            AddCommonMenuItems(arrowContextMenu);
            #endregion

            #region 子面板快捷菜单
            subpanelContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(subpanelContextMenu, TextFormattingMode.Display);

            MenuItem cmiSwitchDirection = new MenuItem() { Header = "指向切换(_S)" };
            cmiSwitchDirection.InputGestureText = "H";
            cmiSwitchDirection.Click += new RoutedEventHandler(cmiSwitchDirection_Click);

            MenuItem subPanelHeader = BuildMenuHeader("子面板");
            subPanelHeader.Click += new RoutedEventHandler(subPanelHeader_Click);
            subpanelContextMenu.Items.Add(subPanelHeader);

            subpanelContextMenu.Items.Add(new Separator());
            subpanelContextMenu.Items.Add(cmiSwitchDirection);
            #endregion

            #region 叶子面板快捷菜单
            leafPanelContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(leafPanelContextMenu, TextFormattingMode.Display);

            MenuItem leafPanelHeader = BuildMenuHeader("叶面板");
            leafPanelHeader.Click += new RoutedEventHandler(leafPanelHeader_Click);

            MenuItem leafPanelInsert = new MenuItem()
            {
                Header = "插入叶(_I)",
                InputGestureText = "K",
                ToolTip = "并立即开始编辑文本"
            };
            leafPanelInsert.Click += new RoutedEventHandler(leafPanelInsert_Click);

            MenuItem leafPanelInsertNextLevel = new MenuItem()
            {
                Header = "插入分支(_B)",
                InputGestureText = "N",
                ToolTip = "并立即开始编辑文本"
            };
            leafPanelInsertNextLevel.Click += new RoutedEventHandler(leafPanelInsertNextLevel_Click);

            Image imgCut = new Image();
            imgCut.Width = imgCut.Height = 16;
            imgCut.Source = new BitmapImage(new Uri(@"pack://Application:,,,/LunarPresentation;component/Resources/Icons/CutHS.png"));

            MenuItem leafPanelCut = new MenuItem()
            {
                Header = "剪切(_X)",
                InputGestureText = "Ctrl+X",
                Icon = imgCut
            };
            leafPanelCut.Click += new RoutedEventHandler(leafPanelCut_Click);

            Image imgCopy = new Image();
            imgCopy.Width = imgCut.Height = 16;
            imgCopy.Source = new BitmapImage(new Uri(@"pack://Application:,,,/LunarPresentation;component/Resources/Icons/CopyHS.png"));

            MenuItem leafPanelCopy = new MenuItem()
            {
                Header = "复制(_C)",
                InputGestureText = "Ctrl+C",
                Icon = imgCopy
            };
            leafPanelCopy.Click += new RoutedEventHandler(leafPanelCopy_Click);

            Image imgPaste = new Image();
            imgPaste.Width = imgPaste.Height = 16;
            imgPaste.Source = new BitmapImage(new Uri(@"pack://Application:,,,/LunarPresentation;component/Resources/Icons/PasteHS.png"));

            MenuItem leafPanelPaste = new MenuItem()
            {
                Header = "粘贴(_P)",
                InputGestureText = "Ctrl+V",
                Icon = imgPaste
            };
            leafPanelPaste.Click += new RoutedEventHandler(leafPanelPaste_Click);

            MenuItem leafPanelDelete = new MenuItem() { Header = "删除(_D)", InputGestureText = "Delete" };
            leafPanelDelete.Click += new RoutedEventHandler(leafPanelDelete_Click);

            //MenuItem leafPanelMoveUp = new MenuItem() { Header = "上移(_U)" };
            //leafPanelMoveUp.Click += new RoutedEventHandler(leafPanelMoveUp_Click);

            //MenuItem leafPanelMoveDown = new MenuItem() { Header = "下移(_D)" };
            //leafPanelMoveDown.Click += new RoutedEventHandler(leafPanelMoveDown_Click);

            //MenuItem leafPanelMoveLeft = new MenuItem() { Header = "左移(_L)" };
            //leafPanelMoveLeft.Click += new RoutedEventHandler(leafPanelMoveLeft_Click);

            //MenuItem leafPanelMoveRight = new MenuItem() { Header = "右移(_R)" };
            //leafPanelMoveRight.Click += new RoutedEventHandler(leafPanelMoveRight_Click);

            MenuItem leafMoving = new MenuItem()
            {
                Height = double.NaN,
                VerticalContentAlignment = VerticalAlignment.Center,
                InputGestureText = "上：C→ys\r\n下：C→yx\r\n中：C→yc\r\n左：C→yz\r\n右：C→yy"
            };
            Grid leafMovingGrid = new Grid();
            for (int i = 0; i < 3; i++)
            {
                leafMovingGrid.ColumnDefinitions.Add(new ColumnDefinition());
                leafMovingGrid.RowDefinitions.Add(new RowDefinition());
            }

            //五个移动按钮。
            Button btnMoveLeafUp = new Button()
            {
                ToolTip = "叶面板上移一位",
                Foreground = Brushes.Transparent,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            leafMovingGrid.Children.Add(btnMoveLeafUp);

            Grid.SetColumn(btnMoveLeafUp, 1); Grid.SetRow(btnMoveLeafUp, 0);
            Image imgMoveLeafUp = new Image() { Width = 16, Height = 16 };
            imgMoveLeafUp.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/LeafImgs/MoveLeafUp.png"));
            btnMoveLeafUp.Content = imgMoveLeafUp;
            btnMoveLeafUp.Click += new RoutedEventHandler(btnMoveLeafUp_Click);


            Button btnMoveLeafDown = new Button()
            {
                ToolTip = "叶面板下移一位",
                Foreground = Brushes.Transparent,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            leafMovingGrid.Children.Add(btnMoveLeafDown);

            Grid.SetColumn(btnMoveLeafDown, 1); Grid.SetRow(btnMoveLeafDown, 2);
            Image imgMoveLeafDown = new Image() { Width = 16, Height = 16 };
            imgMoveLeafDown.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/LeafImgs/MoveLeafDown.png"));
            btnMoveLeafDown.Content = imgMoveLeafDown;
            btnMoveLeafDown.Click += new RoutedEventHandler(btnMoveLeafDown_Click);


            Button btnMoveLeafLeft = new Button()
            {
                ToolTip = "叶面板左移一层",
                Foreground = Brushes.Transparent,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            leafMovingGrid.Children.Add(btnMoveLeafLeft);

            Grid.SetColumn(btnMoveLeafLeft, 0); Grid.SetRow(btnMoveLeafLeft, 1);
            Image imgMoveLeafLeft = new Image() { Width = 16, Height = 16 };
            imgMoveLeafLeft.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/LeafImgs/MoveLeafLeft.png"));
            btnMoveLeafLeft.Content = imgMoveLeafLeft;
            btnMoveLeafLeft.Click += new RoutedEventHandler(btnMoveLeafLeft_Click);


            Button btnMoveLeafRight = new Button()
            {
                ToolTip = "叶面板右移一层",
                Foreground = Brushes.Transparent,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            leafMovingGrid.Children.Add(btnMoveLeafRight);

            Grid.SetColumn(btnMoveLeafRight, 2); Grid.SetRow(btnMoveLeafRight, 1);
            Image imgMoveLeafRight = new Image() { Width = 16, Height = 16 };
            imgMoveLeafRight.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/LeafImgs/MoveLeafRight.png"));
            btnMoveLeafRight.Content = imgMoveLeafRight;
            btnMoveLeafRight.Click += new RoutedEventHandler(btnMoveLeafRight_Click);

            Button btnSelectRootLeaf = new Button()
            {
                ToolTip = "选中叶首面板",
                Margin = new Thickness(2),
                Foreground = Brushes.Transparent,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };

            leafMovingGrid.Children.Add(btnSelectRootLeaf);

            Grid.SetColumn(btnSelectRootLeaf, 1); Grid.SetRow(btnSelectRootLeaf, 1);
            Image imgSelectRootLeaf = new Image() { Width = 16, Height = 16 };
            imgSelectRootLeaf.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/LeafImgs/Center.png"));
            btnSelectRootLeaf.Content = imgSelectRootLeaf;
            btnSelectRootLeaf.Click += new RoutedEventHandler(btnSelectRootLeaf_Click);


            leafMoving.Header = leafMovingGrid;

            leafPanelContextMenu.Items.Add(leafPanelHeader);
            leafPanelContextMenu.Items.Add(new Separator());
            leafPanelContextMenu.Items.Add(leafPanelInsert);
            leafPanelContextMenu.Items.Add(leafPanelInsertNextLevel);

            leafPanelContextMenu.Items.Add(new Separator());
            //leafPanelContextMenu.Items.Add(leafPanelMoveUp);
            //leafPanelContextMenu.Items.Add(leafPanelMoveDown);

            //leafPanelContextMenu.Items.Add(leafPanelMoveLeft);
            //leafPanelContextMenu.Items.Add(leafPanelMoveRight);

            leafPanelContextMenu.Items.Add(leafMoving);

            leafPanelContextMenu.Items.Add(new Separator());
            leafPanelContextMenu.Items.Add(leafPanelCut);
            leafPanelContextMenu.Items.Add(leafPanelCopy);
            leafPanelContextMenu.Items.Add(leafPanelPaste);
            leafPanelContextMenu.Items.Add(leafPanelDelete);
            #endregion

            #region 网格子面板快捷菜单
            gridPanelContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(gridPanelContextMenu, TextFormattingMode.Display);

            //与子面板同名菜单功能相同。
            MenuItem cmiSwitchDirection2 = new MenuItem() { Header = "指向切换(_S)" };
            cmiSwitchDirection2.InputGestureText = "H";
            cmiSwitchDirection2.Click += new RoutedEventHandler(cmiSwitchDirection_Click);

            MenuItem gridPanelHeader = BuildMenuHeader("网格子面板");
            gridPanelHeader.Click += new RoutedEventHandler(subPanelHeader_Click);
            gridPanelContextMenu.Items.Add(gridPanelHeader);

            MenuItem cmiGridOperations = new MenuItem();
            cmiGridOperations.Height = double.NaN;
            Grid g = new Grid();
            for (int i = 0; i < 3; i++)
            {
                g.RowDefinitions.Add(new RowDefinition());
                g.ColumnDefinitions.Add(new ColumnDefinition());
            }

            TextBlock tbBack = new TextBlock() { Text = "网　格\r\n子面板", Foreground = Brushes.DarkGray };

            Border backBorder = new Border()
            {
                BorderBrush = Brushes.Red,
                BorderThickness = new Thickness(1),
                Margin = new Thickness(4),
                Padding = new Thickness(4),
                Child = tbBack
            };
            Grid.SetColumn(backBorder, 1); Grid.SetRow(backBorder, 1);
            g.Children.Add(backBorder);

            //下面是九个按钮。
            #region 插入行（上）
            Button btnInsertRowAtTop = new Button()
                {
                    ToolTip = "插入行（上）",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            Grid.SetColumn(btnInsertRowAtTop, 1); Grid.SetRow(btnInsertRowAtTop, 0);
            Image imgInsertTop = new Image() { Width = 16, Height = 16 };
            imgInsertTop.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/InsertLineAtTop.png"));
            btnInsertRowAtTop.Content = imgInsertTop;
            btnInsertRowAtTop.Click += new RoutedEventHandler(btnInsertRowAtTop_Click);
            #endregion

            #region 插入行（下）
            Button btnInsertRowAtBottom = new Button()
                {
                    ToolTip = "插入行（下）",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            Grid.SetColumn(btnInsertRowAtBottom, 1); Grid.SetRow(btnInsertRowAtBottom, 2);
            Image imgInsertBottom = new Image() { Width = 16, Height = 16 };
            imgInsertBottom.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/InsertLineAtBottom.png"));
            btnInsertRowAtBottom.Content = imgInsertBottom;
            btnInsertRowAtBottom.Click += new RoutedEventHandler(btnInsertRowAtBottom_Click);
            #endregion

            #region 插入列（左）
            Button btnInsertColumnAtLeft = new Button()
                {
                    ToolTip = "插入列（左）",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            Grid.SetColumn(btnInsertColumnAtLeft, 0); Grid.SetRow(btnInsertColumnAtLeft, 1);
            Image imgInsertLeft = new Image() { Width = 16, Height = 16 };
            imgInsertLeft.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/InsertColumnAtLeft.png"));
            btnInsertColumnAtLeft.Content = imgInsertLeft;
            btnInsertColumnAtLeft.Click += new RoutedEventHandler(btnInsertColumnAtLeft_Click);
            #endregion

            #region 插入列（右）
            Button btnInsertColumnAtRight = new Button()
                {
                    ToolTip = "插入列（右）",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            Grid.SetColumn(btnInsertColumnAtRight, 2); Grid.SetRow(btnInsertColumnAtRight, 1);
            Image imgInsertRight = new Image() { Width = 16, Height = 16 };
            imgInsertRight.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/InsertColumnAtRight.png"));
            btnInsertColumnAtRight.Content = imgInsertRight;
            btnInsertColumnAtRight.Click += new RoutedEventHandler(btnInsertColumnAtRight_Click);
            #endregion

            #region 向右合并
            Button btnMergeRight = new Button()
                {
                    ToolTip = "向右合并",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            Grid.SetColumn(btnMergeRight, 2); Grid.SetRow(btnMergeRight, 0);
            Image imgMergeRight = new Image() { Width = 16, Height = 16 };
            imgMergeRight.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/MergeToRight.png"));
            btnMergeRight.Content = imgMergeRight;
            btnMergeRight.Click += new RoutedEventHandler(btnMergeRight_Click);
            #endregion

            #region 向下合并
            Button btnMergeBottom = new Button()
                {
                    ToolTip = "向下合并",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            Grid.SetColumn(btnMergeBottom, 0); Grid.SetRow(btnMergeBottom, 2);
            Image imgMergeBottom = new Image() { Width = 16, Height = 16 };
            imgMergeBottom.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/MergeToBottom.png"));
            btnMergeBottom.Content = imgMergeBottom;
            btnMergeBottom.Click += new RoutedEventHandler(btnMergeBottom_Click);
            #endregion

            #region 取消合并
            Button btnMergeCancel = new Button()
                {
                    ToolTip = "取消合并",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            Grid.SetColumn(btnMergeCancel, 1); Grid.SetRow(btnMergeCancel, 1);
            Image imgMergeCancel = new Image() { Width = 16, Height = 16 };
            imgMergeCancel.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/MergeCancel.png"));
            btnMergeCancel.Content = imgMergeCancel;
            btnMergeCancel.Click += new RoutedEventHandler(btnMergeCancel_Click);
            #endregion

            #region 删除行
            Button btnDeleteRow = new Button()
               {
                   ToolTip = "删除行",
                   HorizontalAlignment = HorizontalAlignment.Center,
                   VerticalAlignment = VerticalAlignment.Center
               };
            Grid.SetRow(btnDeleteRow, 0); Grid.SetColumn(btnDeleteRow, 0);
            Image imgDeleteRow = new Image() { Width = 16, Height = 16 };
            imgDeleteRow.Source = new BitmapImage(
                new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/DeleteLine.png"));
            btnDeleteRow.Content = imgDeleteRow;
            btnDeleteRow.Click += new RoutedEventHandler(btnDeleteRow_Click);
            #endregion

            #region 删除列
            Button btnDeleteColumn = new Button()
                {
                    ToolTip = "删除列",
                    HorizontalAlignment = HorizontalAlignment.Center,
                    VerticalAlignment = VerticalAlignment.Center
                };
            Grid.SetColumn(btnDeleteColumn, 2); Grid.SetRow(btnDeleteColumn, 2);
            Image imgDeleteColumn = new Image() { Width = 16, Height = 16 };
            imgDeleteColumn.Source = new BitmapImage(new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/DeleteColumn.png"));
            btnDeleteColumn.Content = imgDeleteColumn;
            btnDeleteColumn.Click += new RoutedEventHandler(btnDeleteColumn_Click);
            #endregion

            g.Children.Add(btnInsertColumnAtLeft);
            g.Children.Add(btnInsertColumnAtRight);
            g.Children.Add(btnInsertRowAtBottom);
            g.Children.Add(btnInsertRowAtTop);
            g.Children.Add(btnMergeBottom);
            g.Children.Add(btnMergeRight);
            g.Children.Add(btnMergeCancel);
            g.Children.Add(btnDeleteColumn);
            g.Children.Add(btnDeleteRow);

            cmiGridOperations.Header = g;

            MenuItem cmiGridPanelAlignment = new MenuItem() { Height = double.NaN, ToolTip = "网格子面板对齐" };
            Widegets.GridPanelAlignButtons gpab = new Widegets.GridPanelAlignButtons();
            gpab.Clicked += new EventHandler<Widegets.GridPanelAlignmentEventArgs>(gpab_Clicked);
            cmiGridPanelAlignment.Header = gpab;

            gridPanelContextMenu.Items.Add(new Separator());
            gridPanelContextMenu.Items.Add(cmiGridOperations);
            gridPanelContextMenu.Items.Add(new Separator());
            gridPanelContextMenu.Items.Add(cmiGridPanelAlignment);
            gridPanelContextMenu.Items.Add(new Separator());
            gridPanelContextMenu.Items.Add(cmiSwitchDirection2);
            #endregion

            #region [矛盾/对立]关系式快捷菜单
            contradictionContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(contradictionContextMenu, TextFormattingMode.Display);

            MenuItem cmiSetContradiction = new MenuItem() { Header = "矛盾关系" };
            cmiSetContradiction.Click += new RoutedEventHandler(cmiSetContradiction_Click);

            MenuItem cmiSetOpposite = new MenuItem() { Header = "对立关系" };
            cmiSetOpposite.Click += new RoutedEventHandler(cmiSetOpposite_Click);

            MenuItem contradictionHeader = BuildMenuHeader("[矛盾/对立]关系式");
            contradictionHeader.Click += new RoutedEventHandler(contradictionHeader_Click);
            contradictionContextMenu.Items.Add(contradictionHeader);

            contradictionContextMenu.Items.Add(new Separator());
            contradictionContextMenu.Items.Add(cmiSetContradiction);
            contradictionContextMenu.Items.Add(cmiSetOpposite);
            AddCommonMenuItems(contradictionContextMenu);
            #endregion

            #region 图片框式快捷菜单
            pictureBoxContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(pictureBoxContextMenu, TextFormattingMode.Display);

            MenuItem cmiSelectImageFile = new MenuItem() { Header = "设置图像文件(_I)", InputGestureText = "Enter" };
            cmiSelectImageFile.Click += new RoutedEventHandler(cmiSelectImageFile_Click);

            MenuItem pictureBoxHeader = BuildMenuHeader("图片框");
            pictureBoxHeader.Click += new RoutedEventHandler(pictureBoxHeader_Click);
            pictureBoxContextMenu.Items.Add(pictureBoxHeader);

            pictureBoxContextMenu.Items.Add(new Separator());
            pictureBoxContextMenu.Items.Add(cmiSelectImageFile);
            AddCommonMenuItems(pictureBoxContextMenu);
            #endregion

            #region 扩展式快捷菜单
            expanderContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(expanderContextMenu, TextFormattingMode.Display);

            MenuItem cmiSetButtonText = new MenuItem()
            {
                Header = "设置折叠按钮文本(_T)",
                ToolTip = "即公式的备注（注释）文本",
                InputGestureText = "M",
            };
            cmiSetButtonText.Click += new RoutedEventHandler(setButtonText_Click);

            MenuItem cmiSwitchSlavePanelIndent = new MenuItem() { Header = "切换附属面板缩进(_I)" };
            cmiSwitchSlavePanelIndent.Click += new RoutedEventHandler(cmiSwitchSlavePanelIndent_Click);

            MenuItem cmiSwitchCollapse = new MenuItem() { Header = "展开、折叠(_Z)", InputGestureText = "Z" };
            cmiSwitchCollapse.Click += new RoutedEventHandler(cmiSwitchCollapse_Click);

            MenuItem cmiExpanderTurnoff = new MenuItem() { Header = "翻转子面板(_T)" };
            cmiExpanderTurnoff.Click += new RoutedEventHandler(cmiExpanderTurnoff_Click);

            MenuItem expanderHeader = BuildMenuHeader("扩展式");
            expanderHeader.Click += new RoutedEventHandler(expanderHeader_Click);
            expanderContextMenu.Items.Add(expanderHeader);

            expanderContextMenu.Items.Add(new Separator());
            expanderContextMenu.Items.Add(cmiSetButtonText);
            expanderContextMenu.Items.Add(cmiSwitchSlavePanelIndent);
            expanderContextMenu.Items.Add(new Separator());
            expanderContextMenu.Items.Add(cmiExpanderTurnoff);
            expanderContextMenu.Items.Add(cmiSwitchCollapse);
            AddCommonMenuItems(expanderContextMenu);
            #endregion

            #region 文本块快捷菜单
            baseTextContextMenu = new ContextMenu();
            baseTextContextMenu.Opened += new RoutedEventHandler(baseTextContextMenu_Opened);
            TextOptions.SetTextFormattingMode(baseTextContextMenu, TextFormattingMode.Display);

            MenuItem cmiEditBaseText = new MenuItem() { Header = "编辑文本(_E)", InputGestureText = "Enter" };
            cmiEditBaseText.Click += new RoutedEventHandler(cmiEditBaseText_Click);

            MenuItem cmiSwitchShowBackText = new MenuItem() { Header = "切换前∕后台文本(_S)", InputGestureText = "-" };
            cmiSwitchShowBackText.Click += new RoutedEventHandler(cmiSwitchShowBackText_Click);

            MenuItem cmiClearBaseText = new MenuItem() { Header = "清除文本(_R)", InputGestureText = "Backspace" };
            cmiClearBaseText.Click += new RoutedEventHandler(cmiClearBaseText_Click);

            MenuItem cmiCopyBaseText = new MenuItem() { Header = "复制文本(_C)" };
            cmiCopyBaseText.Click += new RoutedEventHandler(cmiCopyBaseText_Click);

            MenuItem cmiPasteAdd = new MenuItem() { Header = "追加粘贴(_A)" };
            cmiPasteAdd.Click += new RoutedEventHandler(cmiPasteAdd_Click);

            MenuItem cmiPasteOverride = new MenuItem() { Header = "覆盖粘贴(_O)" };
            cmiPasteOverride.Click += new RoutedEventHandler(cmiPasteOverride_Click);

            MenuItem baseTextHeader = BuildMenuHeader("文本块");
            baseTextHeader.Click += new RoutedEventHandler(baseTextHeader_Click);
            baseTextContextMenu.Items.Add(baseTextHeader);

            MenuItem cmmitextMaxWidth = new MenuItem()
            {
                Header = "★设置文本块最大宽度(_M)",
                ToolTip = "对此文档中所有文本块均产生限制，\r\n除非此文本块被设置为“不折行”。"
            };
            cmmitextMaxWidth.Click += new RoutedEventHandler(cmmitextMaxWidth_Click);
            baseTextContextMenu.Items.Add(new Separator());
            baseTextContextMenu.Items.Add(cmmitextMaxWidth);

            #region 遮罩菜单项
            MenuItem cmmiMask = new MenuItem() { Header = "遮罩(_M)" };

            MenuItem cmiLeftBias = new MenuItem() { Header = "左斜线(_L)", InputGestureText = "/" };
            cmiLeftBias.Click += new RoutedEventHandler(cmiLeftBias_Click);

            MenuItem cmiRightBias = new MenuItem() { Header = "右斜线(_R)", InputGestureText = "C→yxx" };
            cmiRightBias.Click += new RoutedEventHandler(cmiRightBias_Click);

            MenuItem cmiHLine = new MenuItem() { Header = "横线(_H)", InputGestureText = "C→hx" };
            cmiHLine.Click += new RoutedEventHandler(cmiHLine_Click);

            MenuItem cmiVLine = new MenuItem() { Header = "竖线(_V)", InputGestureText = "C→sx" };
            cmiVLine.Click += new RoutedEventHandler(cmiVLine_Click);

            MenuItem cmiHVLine = new MenuItem() { Header = "网格线(_G)", InputGestureText = "C→wgx" };
            cmiHVLine.Click += new RoutedEventHandler(cmiHVLine_Click);

            MenuItem cmiNoneMask = new MenuItem() { Header = "无遮罩(_N)", InputGestureText = "C→wzz" };
            cmiNoneMask.Click += new RoutedEventHandler(cmiNoneMask_Click);
            #endregion

            #region 任务列表菜单项

            MenuItem cmmiTaskList = new MenuItem() { Header = "任务列表项(_T)" };

            MenuItem cmiNotStartedTask = new MenuItem() { Header = "准备(_B)", InputGestureText = "C→rwzb" };
            cmiNotStartedTask.Click += new RoutedEventHandler(cmiNotStartedTask_Click);

            MenuItem cmiRunningTask = new MenuItem() { Header = "运行(_R)", InputGestureText = "C→rwyx" };
            cmiRunningTask.Click += new RoutedEventHandler(cmiRunningTask_Click);

            MenuItem cmiFinishedTask = new MenuItem() { Header = "完成(_F)", InputGestureText = "C→rwwc" };
            cmiFinishedTask.Click += new RoutedEventHandler(cmiFinishedTask_Click);

            MenuItem cmiAbortTask = new MenuItem() { Header = "失败(_A)", InputGestureText = "C→rwsb" };
            cmiAbortTask.Click += new RoutedEventHandler(cmiAbortTask_Click);

            MenuItem cmiPauseTask = new MenuItem() { Header = "暂停(_P)", InputGestureText = "C→rwzt" };
            cmiPauseTask.Click += new RoutedEventHandler(cmiPauseTask_Click);

            MenuItem cmiNotTask = new MenuItem() { Header = "取消(_C)", InputGestureText = "C→rwqx" };
            cmiNotTask.Click += new RoutedEventHandler(cmiNotTask_Click);

            cmmiTaskList.Items.Add(cmiNotStartedTask);
            cmmiTaskList.Items.Add(cmiRunningTask);
            cmmiTaskList.Items.Add(cmiPauseTask);
            cmmiTaskList.Items.Add(cmiFinishedTask);
            cmmiTaskList.Items.Add(cmiAbortTask);
            cmmiTaskList.Items.Add(new Separator());
            cmmiTaskList.Items.Add(cmiNotTask);
            #endregion

            #region 自动折行菜单项

            MenuItem cmmiAutoWrap = new MenuItem() { Header = "自动折行(_W)" };

            cmiAutoWrap = new MenuItem() { Header = "自动折行(_W)" };
            cmiAutoWrap.Click += new RoutedEventHandler(cmiAutoWrap_Click);

            cmiNoWrap = new MenuItem() { Header = "不折行(_N)" };
            cmiNoWrap.Click += new RoutedEventHandler(cmiNoWrap_Click);

            cmiWrapFromDoc = new MenuItem() { Header = "随文档(_D)" };
            cmiWrapFromDoc.Click += new RoutedEventHandler(cmiWrapFromDoc_Click);

            cmmiAutoWrap.Items.Add(cmiWrapFromDoc);
            cmmiAutoWrap.Items.Add(cmiAutoWrap);
            cmmiAutoWrap.Items.Add(cmiNoWrap);
            #endregion

            baseTextContextMenu.Items.Add(new Separator());
            baseTextContextMenu.Items.Add(cmiSwitchShowBackText);
            baseTextContextMenu.Items.Add(new Separator());

            cmmiMask.Items.Add(cmiLeftBias);
            cmmiMask.Items.Add(cmiRightBias);
            cmmiMask.Items.Add(cmiNoneMask);
            cmmiMask.Items.Add(new Separator());
            cmmiMask.Items.Add(cmiHLine);
            cmmiMask.Items.Add(cmiVLine);
            cmmiMask.Items.Add(cmiHVLine);

            baseTextContextMenu.Items.Add(cmmiMask);
            baseTextContextMenu.Items.Add(cmmiTaskList);
            baseTextContextMenu.Items.Add(cmmiAutoWrap);

            baseTextContextMenu.Items.Add(new Separator());
            baseTextContextMenu.Items.Add(cmiEditBaseText);
            baseTextContextMenu.Items.Add(cmiClearBaseText);
            baseTextContextMenu.Items.Add(cmiCopyBaseText);
            baseTextContextMenu.Items.Add(cmiPasteAdd);
            baseTextContextMenu.Items.Add(cmiPasteOverride);
            AddCommonMenuItems(baseTextContextMenu);
            #endregion

            #region 括弧(不要与括弧式混淆)快捷菜单
            braceContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(braceContextMenu, TextFormattingMode.Display);

            MenuItem cmiBraceStart = new MenuItem() { Header = "首括弧(_S)" };
            cmiBraceStart.Click += new RoutedEventHandler(cmiBraceStart_Click);

            MenuItem cmiBraceEnd = new MenuItem() { Header = "尾括弧(_E)" };
            cmiBraceEnd.Click += new RoutedEventHandler(cmiBraceEnd_Click);

            //将两个括弧之间的公式嵌入一个行中——菜单项。
            MenuItem cmiBraceToRow = new MenuItem()
                {
                    Header = "配对嵌入“行”(_R)",
                    ToolTip = "将首、尾两个“括弧”之间的公式嵌入一个“行”中"
                };
            cmiBraceToRow.Click += new RoutedEventHandler(cmiBraceToRow_Click);

            MenuItem braceheader = BuildMenuHeader("括弧");
            braceheader.Click += new RoutedEventHandler(braceheader_Click);
            braceContextMenu.Items.Add(braceheader);

            braceContextMenu.Items.Add(new Separator());
            braceContextMenu.Items.Add(cmiBraceStart);
            braceContextMenu.Items.Add(cmiBraceEnd);
            braceContextMenu.Items.Add(new Separator());
            braceContextMenu.Items.Add(cmiBraceToRow);
            AddCommonMenuItems(braceContextMenu);
            #endregion

            #region 括弧式快捷菜单
            bracketContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(bracketContextMenu, TextFormattingMode.Display);

            MenuItem cmiBracketDirection = new MenuItem() { Header = "指向切换(_D)", InputGestureText = "H" };
            cmiBracketDirection.Click += new RoutedEventHandler(cmiBracketDirection_Click);

            MenuItem cmiBracketTurnoff = new MenuItem() { Header = "翻转子面板(_T)" };
            cmiBracketTurnoff.Click += new RoutedEventHandler(cmiBracketTurnoff_Click);

            MenuItem bracketHeader = BuildMenuHeader("括弧式");
            bracketHeader.Click += new RoutedEventHandler(bracketHeader_Click);
            bracketContextMenu.Items.Add(bracketHeader);

            bracketContextMenu.Items.Add(new Separator());
            bracketContextMenu.Items.Add(cmiBracketDirection);
            bracketContextMenu.Items.Add(cmiBracketTurnoff);
            AddCommonMenuItems(bracketContextMenu);
            #endregion

            #region 三角式快捷菜单
            triangleContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(triangleContextMenu, TextFormattingMode.Display);

            MenuItem triangleHeader = BuildMenuHeader("三角式");
            triangleHeader.Click += new RoutedEventHandler(triangleHeader_Click);

            triangleContextMenu.Items.Add(triangleHeader);
            AddCommonMenuItems(triangleContextMenu);
            #endregion

            #region 上下标式快捷菜单
            markTextContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(markTextContextMenu, TextFormattingMode.Display);

            MenuItem markTextHeader = BuildMenuHeader("上下标式");
            markTextHeader.Click += new RoutedEventHandler(markTextHeader_Click);

            MenuItem cmiEditMarkText = new MenuItem() { Header = "编辑上下标文本(_E)", InputGestureText = "Enter" };
            cmiEditMarkText.Click += new RoutedEventHandler(cmiEditMarkText_Click);

            MenuItem cmiSwitchMarks = new MenuItem() { Header = "交换上下标文本(_W)" };
            cmiSwitchMarks.Click += new RoutedEventHandler(cmiSwitchMarks_Click);

            markTextContextMenu.Items.Add(markTextHeader);
            markTextContextMenu.Items.Add(new Separator());

            MenuItem cmmMidLine = new MenuItem() { Header = "辅助线(_L)" };
            MenuItem cmiMidLineLeftArrow = new MenuItem() { Header = "左箭头(_L)" };
            cmiMidLineLeftArrow.Click += new RoutedEventHandler(cmiMidLineLeftArrow_Click);

            MenuItem cmiMidLineRightArrow = new MenuItem() { Header = "右箭头(_R)" };
            cmiMidLineRightArrow.Click += new RoutedEventHandler(cmiMidLineRightArrow_Click);

            MenuItem cmiMidLineSingleLine = new MenuItem() { Header = "单线(_S)" };
            cmiMidLineSingleLine.Click += new RoutedEventHandler(cmiMidLineSingleLine_Click);

            MenuItem cmiMidLineDoubleLine = new MenuItem() { Header = "双线(_D)" };
            cmiMidLineDoubleLine.Click += new RoutedEventHandler(cmiMidLineDoubleLine_Click);

            MenuItem cmiMidLineDoubleArrow = new MenuItem() { Header = "双箭头(_W)" };
            cmiMidLineDoubleArrow.Click += new RoutedEventHandler(cmiMidLineDoubleArrow_Click);

            MenuItem cmiMidLineNone = new MenuItem() { Header = "无(_N)" };
            cmiMidLineNone.Click += new RoutedEventHandler(cmiMidLineNone_Click);

            cmmMidLine.Items.Add(cmiMidLineRightArrow);
            cmmMidLine.Items.Add(cmiMidLineLeftArrow);
            cmmMidLine.Items.Add(cmiMidLineDoubleArrow);
            cmmMidLine.Items.Add(new Separator());
            cmmMidLine.Items.Add(cmiMidLineSingleLine);
            cmmMidLine.Items.Add(cmiMidLineDoubleLine);
            cmmMidLine.Items.Add(new Separator());
            cmmMidLine.Items.Add(cmiMidLineNone);

            markTextContextMenu.Items.Add(cmmMidLine);

            markTextContextMenu.Items.Add(new Separator());
            markTextContextMenu.Items.Add(cmiEditMarkText);
            markTextContextMenu.Items.Add(cmiSwitchMarks);
            AddCommonMenuItems(markTextContextMenu);
            #endregion

            #region 属种关系式快捷菜单
            genuspaciesContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(genuspaciesContextMenu, TextFormattingMode.Display);

            MenuItem genuspaciesHeader = BuildMenuHeader("属种关系式");
            genuspaciesHeader.Click += new RoutedEventHandler(genuspaciesHeader_Click);

            genuspaciesContextMenu.Items.Add(genuspaciesHeader);
            AddCommonMenuItems(genuspaciesContextMenu);
            #endregion

            #region 交叉关系式快捷菜单
            crossContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(crossContextMenu, TextFormattingMode.Display);

            MenuItem crossHeader = BuildMenuHeader("交叉关系式");
            crossHeader.Click += new RoutedEventHandler(crossHeader_Click);

            crossContextMenu.Items.Add(crossHeader);
            AddCommonMenuItems(crossContextMenu);
            #endregion

            #region 网格式快捷菜单
            gridContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(gridContextMenu, TextFormattingMode.Display);

            MenuItem gridHeader = BuildMenuHeader("网格式");
            gridHeader.Click += new RoutedEventHandler(gridHeader_Click);

            MenuItem cmiGridAlignmentAllPanels = new MenuItem() { Height = double.NaN, ToolTip = "网格子面板对齐" };
            Widegets.GridPanelAlignButtons gpab2 = new Widegets.GridPanelAlignButtons() { IsCheckBoxsVisible = false };
            gpab2.Clicked += new EventHandler<Widegets.GridPanelAlignmentEventArgs>(gpab2_Clicked);
            cmiGridAlignmentAllPanels.Header = gpab2;

            gridContextMenu.Items.Add(gridHeader);
            gridContextMenu.Items.Add(new Separator());
            gridContextMenu.Items.Add(cmiGridAlignmentAllPanels);
            AddCommonMenuItems(gridContextMenu);
            #endregion

            #region 树快捷菜单
            treeContextMenu = new ContextMenu();
            treeContextMenu.Opened += new RoutedEventHandler(treeContextMenu_Opened);
            TextOptions.SetTextFormattingMode(treeContextMenu, TextFormattingMode.Display);

            MenuItem treeHeader = BuildMenuHeader("树");
            treeHeader.Click += new RoutedEventHandler(treeHeader_Click);

            MenuItem cmiTreeLineType = new MenuItem() { Header = "线型(_L)" };

            cmiDefTreeLineType = new MenuItem() { Header = "粗曲线(_B)" };
            cmiDefTreeLineType.Click += new RoutedEventHandler(cmiDefTreeLineType_Click);

            cmiSingleTreeLineType = new MenuItem() { Header = "细直线(_S)" };
            cmiSingleTreeLineType.Click += new RoutedEventHandler(cmiSingleTreeLineType_Click);

            cmiOrgTreeLineType = new MenuItem() { Header = "直框线(_O)" };
            cmiOrgTreeLineType.Click += new RoutedEventHandler(cmiOrgTreeLineType_Click);

            treeContextMenu.Items.Add(treeHeader);
            treeContextMenu.Items.Add(new Separator());
            treeContextMenu.Items.Add(cmiTreeLineType);
            cmiTreeLineType.Items.Add(cmiDefTreeLineType);
            cmiTreeLineType.Items.Add(cmiSingleTreeLineType);
            cmiTreeLineType.Items.Add(cmiOrgTreeLineType);
            AddCommonMenuItems(treeContextMenu);
            #endregion

            #region 行快捷菜单
            rowContextMenu = new ContextMenu();
            TextOptions.SetTextFormattingMode(rowContextMenu, TextFormattingMode.Display);

            MenuItem rowHeader = BuildMenuHeader("行");
            rowHeader.Click += new RoutedEventHandler(rowHeader_Click);

            MenuItem cmiRowDirection = new MenuItem() { Header = "指向切换(_D)", InputGestureText = "H" };
            cmiRowDirection.Click += new RoutedEventHandler(cmiRowDirection_Click);

            MenuItem cmiRowOperations = new MenuItem() { Height = double.NaN };
            var rowOperationControl = new Widegets.RowOperationsControl();
            rowOperationControl.CmdButtonClicked += new EventHandler<Widegets.RowOperationButtonClickedEventArgs>(rowOperationControl_CmdButtonClicked);
            cmiRowOperations.Header = rowOperationControl;

            rowContextMenu.Items.Add(rowHeader);
            rowContextMenu.Items.Add(new Separator());
            rowContextMenu.Items.Add(cmiRowOperations);
            rowContextMenu.Items.Add(new Separator());
            rowContextMenu.Items.Add(cmiRowDirection);
            AddCommonMenuItems(rowContextMenu);
            #endregion

            #region 富文本块快捷菜单
            richTextContextMenu = new ContextMenu();
            richTextContextMenu.Opened += new RoutedEventHandler(richTextContextMenu_Opened);
            TextOptions.SetTextFormattingMode(richTextContextMenu, TextFormattingMode.Display);

            MenuItem richTextHeader = BuildMenuHeader("富文本块");
            richTextHeader.Click += new RoutedEventHandler(richTextHeader_Click);

            richTextContextMenu.Items.Add(richTextHeader);
            richTextContextMenu.Items.Add(new Separator());

            #region 自动折行菜单项

            MenuItem cmmiRTAutoWrap = new MenuItem() { Header = "自动折行(_W)" };

            cmiRTAutoWrap = new MenuItem() { Header = "自动折行(_W)" };
            cmiRTAutoWrap.Click += new RoutedEventHandler(cmiRTAutoWrap_Click);

            cmiRTNoWrap = new MenuItem() { Header = "不折行(_N)" };
            cmiRTNoWrap.Click += new RoutedEventHandler(cmiRTNoWrap_Click);

            cmiRTWrapFromDoc = new MenuItem() { Header = "随文档(_D)" };
            cmiRTWrapFromDoc.Click += new RoutedEventHandler(cmiRTWrapFromDoc_Click);

            cmmiRTAutoWrap.Items.Add(cmiRTWrapFromDoc);
            cmmiRTAutoWrap.Items.Add(cmiRTAutoWrap);
            cmmiRTAutoWrap.Items.Add(cmiRTNoWrap);
            #endregion

            MenuItem cmmiRTEdit = new MenuItem()
            {
                Header = "编辑富文本块(_E)",
                InputGestureText = "Enter",
            };
            cmmiRTEdit.Click += new RoutedEventHandler(cmmiRTEdit_Click);

            richTextContextMenu.Items.Add(cmmiRTEdit);
            richTextContextMenu.Items.Add(cmmiRTAutoWrap);
            AddCommonMenuItems(richTextContextMenu);
            #endregion
        }

        #region 括弧快捷菜单事件处理器

        static void cmiBraceToRow_Click(object sender, RoutedEventArgs e)
        {
            Command.BraceToRow();
        }

        static void cmiBraceEnd_Click(object sender, RoutedEventArgs e)
        {
            Command.BraceLocationSetup(Expressions.Brace.BraceLocation.End);
        }

        static void cmiBraceStart_Click(object sender, RoutedEventArgs e)
        {
            Command.BraceLocationSetup(Expressions.Brace.BraceLocation.Start);
        }

        static void braceheader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("brace.htm");
        }
        #endregion

        #region 树菜单处理器

        private static MenuItem cmiDefTreeLineType;

        private static MenuItem cmiSingleTreeLineType;

        private static MenuItem cmiOrgTreeLineType;

        static void treeContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;
            Expressions.Tree tree = Globals.selectedEditor.SelectedExpression as Expressions.Tree;
            if (tree == null) return;

            Image img = new Image();
            img.Width = img.Height = 16;
            img.Source = new BitmapImage(new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/Selected.png"));

            switch (tree.LineType)
            {
                case Expressions.Tree.TreeLineType.Default:
                    {
                        cmiDefTreeLineType.Icon = img;
                        cmiSingleTreeLineType.Icon = null;
                        cmiOrgTreeLineType.Icon = null;
                        break;
                    }
                case Expressions.Tree.TreeLineType.Single:
                    {
                        cmiDefTreeLineType.Icon = null;
                        cmiSingleTreeLineType.Icon = img;
                        cmiOrgTreeLineType.Icon = null;
                        break;
                    }
                case Expressions.Tree.TreeLineType.Orgnization:
                    {
                        cmiDefTreeLineType.Icon = null;
                        cmiSingleTreeLineType.Icon = null;
                        cmiOrgTreeLineType.Icon = img;
                        break;
                    }
            }
        }

        static void cmiOrgTreeLineType_Click(object sender, RoutedEventArgs e)
        {
            //组织结构图式线（框线）。
            SetTreeLineType(Expressions.Tree.TreeLineType.Orgnization);
        }

        static void cmiSingleTreeLineType_Click(object sender, RoutedEventArgs e)
        {
            //单线。
            SetTreeLineType(Expressions.Tree.TreeLineType.Single);
        }

        static void cmiDefTreeLineType_Click(object sender, RoutedEventArgs e)
        {
            //粗线（默认）。
            SetTreeLineType(Expressions.Tree.TreeLineType.Default);
        }

        private static void SetTreeLineType(Expressions.Tree.TreeLineType newLineType)
        {
            if (Globals.selectedEditor == null) return;
            if (Globals.selectedEditor.SelectedExpression == null) return;

            Expressions.Tree tree = Globals.selectedEditor.SelectedExpression as Expressions.Tree;
            if (tree == null) return;

            if (tree.LineType == newLineType) return;

            ModifingItem mi = new ModifingItem();

            Action actLineType = new Action(tree.ExID, -1, tree.GetType().Name, "LineType",
                tree.LineType.ToString(), newLineType.ToString());

            tree.LineType = newLineType;

            mi.Add(actLineType);

            tree.Editor.Do(mi);
            tree.Editor.IsModified = true;
        }

        #endregion

        #region 叶子面板快捷菜单事件处理器。

        static void btnSelectRootLeaf_Click(object sender, RoutedEventArgs e)
        {
            Command.SelectRootLeafOfATree();
        }

        static void btnMoveLeafRight_Click(object sender, RoutedEventArgs e)
        {
            Command.MoveLeafRight();
        }

        static void btnMoveLeafLeft_Click(object sender, RoutedEventArgs e)
        {
            Command.MoveLeafLeft();
        }

        static void btnMoveLeafDown_Click(object sender, RoutedEventArgs e)
        {
            Command.MoveLeafDown();
        }

        static void btnMoveLeafUp_Click(object sender, RoutedEventArgs e)
        {
            Command.MoveLeafUp();
        }

        //static void leafPanelMoveRight_Click(object sender, RoutedEventArgs e)
        //{
        //    Command.MoveLeafRight();
        //}

        //static void leafPanelMoveLeft_Click(object sender, RoutedEventArgs e)
        //{
        //    Command.MoveLeafLeft();
        //}

        //static void leafPanelMoveDown_Click(object sender, RoutedEventArgs e)
        //{
        //    Command.MoveLeafDown();
        //}

        //static void leafPanelMoveUp_Click(object sender, RoutedEventArgs e)
        //{
        //    Command.MoveLeafUp();
        //}

        static void leafPanelInsertNextLevel_Click(object sender, RoutedEventArgs e)
        {
            Command.InsertAndEditNextLevelLeaf();
        }

        static void leafPanelInsert_Click(object sender, RoutedEventArgs e)
        {
            Command.InsertSameLevelLeaf();
        }

        static void leafPanelDelete_Click(object sender, RoutedEventArgs e)
        {
            Command.CutOrDelete(true);
        }

        static void leafPanelPaste_Click(object sender, RoutedEventArgs e)
        {
            Command.Paste();
        }

        static void leafPanelCopy_Click(object sender, RoutedEventArgs e)
        {
            Command.CopyLeaf();
        }

        static void leafPanelCut_Click(object sender, RoutedEventArgs e)
        {
            Command.CutOrDeleteLeaf(false);
        }

        static void leafPanelHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("leafPanel.htm");
        }
        #endregion

        #region 网格子面板操作按钮处理器

        static void gpab2_Clicked(object sender, Widegets.GridPanelAlignmentEventArgs e)
        {
            Command.GridPanelAlignment(e.NewAlignValue, e.AlignmentTargetType);
        }

        static void gpab_Clicked(object sender, Widegets.GridPanelAlignmentEventArgs e)
        {
            Command.GridPanelAlignment(e.NewAlignValue, e.AlignmentTargetType);
        }

        static void btnDeleteColumn_Click(object sender, RoutedEventArgs e)
        {
            Command.DeleteColumn();
        }

        static void btnDeleteRow_Click(object sender, RoutedEventArgs e)
        {
            Command.DeleteLine();
        }

        static void btnMergeCancel_Click(object sender, RoutedEventArgs e)
        {
            Command.Merge(Command.MergeOperation.Cancel);
        }

        static void btnMergeBottom_Click(object sender, RoutedEventArgs e)
        {
            Command.Merge(Command.MergeOperation.Bottom);
        }

        static void btnMergeRight_Click(object sender, RoutedEventArgs e)
        {
            Command.Merge(Command.MergeOperation.Right);
        }

        static void btnInsertColumnAtRight_Click(object sender, RoutedEventArgs e)
        {
            Command.InsertColumn(false);
        }

        static void btnInsertColumnAtLeft_Click(object sender, RoutedEventArgs e)
        {
            Command.InsertColumn();
        }

        static void btnInsertRowAtBottom_Click(object sender, RoutedEventArgs e)
        {
            Command.InsertLine(false);
        }

        static void btnInsertRowAtTop_Click(object sender, RoutedEventArgs e)
        {
            Command.InsertLine();
        }
        #endregion

        #region 标头菜单项，用以调用帮助文件。

        static void treeHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("tree.htm");
        }

        static void gridHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("gridexpression.htm");
        }

        static void crossHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("logicexpressions.htm");
        }

        static void genuspaciesHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("logicexpressions.htm");
        }

        static void markTextHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("marktext.htm");
        }

        static void triangleHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("triangle.htm");
        }

        static void bracketHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("bracket.htm");
        }

        static void baseTextHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("basetext.htm");
        }

        static void expanderHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("expander.htm");
        }

        static void pictureBoxHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("picturebox.htm");
        }

        static void contradictionHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("logicexpressions.htm");
        }

        static void subPanelHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("subpanel.htm");
        }

        static void arrowheader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("arrow.htm");
        }

        public static void ShowHelpFile(string htmlString)
        {
            //string headerString = @"mk:@MSITStore:";
            string helpFilePath = Globals.installedPath;
            if (helpFilePath.EndsWith("\\") == false)
            {
                helpFilePath += "\\";
            }

            helpFilePath += "Help\\Help.chm";

            if (System.IO.File.Exists(helpFilePath) == false)
            {
                MessageBox.Show("未找到帮助文档，无法继续！",
                    Globals.appName, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            try
            {
                //System.Diagnostics.Process.Start("hh.exe", $"\"{headerString + helpFilePath + "::/" + htmlString}\"");
                WinForm.HelpNavigator navigator
                    = WinForm.HelpNavigator.Topic;
                WinForm.Help.ShowHelp(null,
                    helpFilePath, navigator, htmlString);
            }
            catch (Exception e)
            {
                Dialogs.LunarMsgBox.MsgBoxShow(
                    Globals.appName,
                    "未能载入帮助文档。",
                    "异常信息如下：\r\n" + e.Message,
                    e.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }
        #endregion

        /// <summary>
        /// 用在菜单顶部的画刷。
        /// </summary>
        //static LinearGradientBrush lBrush;

        /// <summary>
        /// 创建表示公式类型的菜单项。
        /// </summary>
        /// <param name="menuName"></param>
        /// <returns></returns>
        static MenuItem BuildMenuHeader(string menuName)
        {
            if (menuName == null || menuName.Length <= 0) return null;

            TextBlock tb = new TextBlock() { Text = menuName + " [?]" };

            Image img = new Image();
            img.Width = img.Height = 16;
            img.Source = new BitmapImage(new Uri(@"pack://Application:,,,/LunarPresentation;component/Resources/Icons/Help.png"));

            if (menuName.Contains("子面板"))
            {
                tb.Foreground = Brushes.HotPink;
                return new MenuItem()
                {
                    Header = tb,
                    Icon = img,
                    BorderBrush = Brushes.Red,
                    BorderThickness = new Thickness(1),
                };
            }
            else
            {
                tb.Foreground = Brushes.Brown;
                return new MenuItem()
                {
                    Header = tb,
                    Icon = img,
                    BorderBrush = Brushes.Red,
                    BorderThickness = new Thickness(1),
                };
            }
        }

        /// <summary>
        /// 创建每个菜单都有的通用菜单项。其中，子面板菜单另作处理。
        /// </summary>
        /// <param name="menu"></param>
        static void AddCommonMenuItems(ContextMenu menu)
        {
            if (menu == null) return;
            menu.Items.Add(new Separator());

            Image imgCut = new Image();
            imgCut.Width = imgCut.Height = 16;
            imgCut.Source = new BitmapImage(new Uri(@"pack://Application:,,,/LunarPresentation;component/Resources/Icons/CutHS.png"));

            MenuItem cmiCutEx = new MenuItem() { Header = "剪切公式(_X)", InputGestureText = "Ctrl+X", Icon = imgCut };
            cmiCutEx.Click += new RoutedEventHandler(cmiCutEx_Click);

            Image imgCopy = new Image();
            imgCopy.Width = imgCopy.Height = 16;
            imgCopy.Source = new BitmapImage(new Uri(@"pack://Application:,,,/LunarPresentation;component/Resources/Icons/CopyHS.png"));

            MenuItem cmiCopyEx = new MenuItem() { Header = "复制公式(_C)", InputGestureText = "Ctrl+C", Icon = imgCopy };
            cmiCopyEx.Click += new RoutedEventHandler(cmiCopyEx_Click);

            Image imgPaste = new Image();
            imgPaste.Width = imgPaste.Height = 16;
            imgPaste.Source = new BitmapImage(new Uri(@"pack://Application:,,,/LunarPresentation;component/Resources/Icons/PasteHS.png"));

            MenuItem cmiPasteEx = new MenuItem() { Header = "粘贴公式(_P)", InputGestureText = "Ctrl+V", Icon = imgPaste };
            cmiPasteEx.Click += new RoutedEventHandler(cmiPasteEx_Click);

            MenuItem cmiCopyToImage = new MenuItem() { Header = "复制成图像(_I)", InputGestureText = "Shift+C" };
            cmiCopyToImage.Click += new RoutedEventHandler(cmiCopyToImage_Click);

            MenuItem cmiAssignToWildcardEx = new MenuItem() { Header = "指定为通配式(_A)" };
            cmiAssignToWildcardEx.Click += new RoutedEventHandler(cmiAssignToWildcardEx_Click);

            MenuItem cmiInsertWildcardEx = new MenuItem() { Header = "插入通配式(_V)", InputGestureText = "V" };
            cmiInsertWildcardEx.Click += new RoutedEventHandler(cmiInsertWildcardEx_Click);

            menu.Items.Add(cmiCutEx);
            menu.Items.Add(cmiCopyEx);
            menu.Items.Add(cmiPasteEx);
            menu.Items.Add(cmiCopyToImage);
            menu.Items.Add(new Separator());
            menu.Items.Add(cmiAssignToWildcardEx);
            menu.Items.Add(cmiInsertWildcardEx);
        }

        #region 所有普通公式的快捷菜单共用的菜单项的处理器。
        static void cmiInsertWildcardEx_Click(object sender, RoutedEventArgs e)
        {
            Command.InsertWildcardExpression();
        }

        static void cmiAssignToWildcardEx_Click(object sender, RoutedEventArgs e)
        {
            Command.AssignWildcardExpression();
        }

        static void cmiCopyToImage_Click(object sender, RoutedEventArgs e)
        {
            Command.CopyToImage();
        }

        static void cmiPasteEx_Click(object sender, RoutedEventArgs e)
        {
            Command.Paste();
        }

        static void cmiCopyEx_Click(object sender, RoutedEventArgs e)
        {
            Command.Copy();
        }

        static void cmiCutEx_Click(object sender, RoutedEventArgs e)
        {
            Command.CutOrDelete();
        }
        #endregion

        #region 上下标式快捷菜单处理器

        static void cmiMidLineNone_Click(object sender, RoutedEventArgs e)
        {
            Command.MarkTextMidLine(Expressions.MarkText.AssistLine.None);
        }

        static void cmiMidLineDoubleArrow_Click(object sender, RoutedEventArgs e)
        {
            Command.MarkTextMidLine(Expressions.MarkText.AssistLine.DoubleArrow);
        }

        static void cmiMidLineDoubleLine_Click(object sender, RoutedEventArgs e)
        {
            Command.MarkTextMidLine(Expressions.MarkText.AssistLine.DoubleLine);
        }

        static void cmiMidLineSingleLine_Click(object sender, RoutedEventArgs e)
        {
            Command.MarkTextMidLine(Expressions.MarkText.AssistLine.SingleLine);
        }

        static void cmiMidLineRightArrow_Click(object sender, RoutedEventArgs e)
        {
            Command.MarkTextMidLine(Expressions.MarkText.AssistLine.RightArrow);
        }

        static void cmiMidLineLeftArrow_Click(object sender, RoutedEventArgs e)
        {
            Command.MarkTextMidLine(Expressions.MarkText.AssistLine.LeftArrow);
        }

        static void cmiSwitchMarks_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchMarks();
        }

        static void cmiEditMarkText_Click(object sender, RoutedEventArgs e)
        {
            Command.SelectInOrEdit();
        }
        #endregion

        #region 括弧式快捷菜单处理器
        static void cmiBracketTurnoff_Click(object sender, RoutedEventArgs e)
        {
            Command.TurnOffMSPanels();
        }

        static void cmiBracketDirection_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchDirection();
        }
        #endregion

        #region 文本块快捷菜单处理器

        static void cmmitextMaxWidth_Click(object sender, RoutedEventArgs e)
        {
            Command.SetMaxTextWidth();
        }

        static void cmiWrapFromDoc_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchExAutoWrap(AutoWrapType.FromDoc);
        }

        static void cmiNoWrap_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchExAutoWrap(AutoWrapType.No);
        }

        static void cmiAutoWrap_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchExAutoWrap(AutoWrapType.Wrap);
        }

        static void baseTextContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is IAutoWrap)
            {
                IAutoWrap autoWrapEx = Globals.selectedEditor.SelectedExpression as IAutoWrap;

                Image img = new Image();
                img.Width = img.Height = 16;
                img.Source = new BitmapImage(new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/Selected.png"));

                switch (autoWrapEx.AutoWrap)
                {
                    case AutoWrapType.FromDoc:
                        {
                            cmiWrapFromDoc.Icon = img;
                            cmiAutoWrap.Icon = null;
                            cmiNoWrap.Icon = null;
                            break;
                        }
                    case AutoWrapType.Wrap:
                        {
                            cmiWrapFromDoc.Icon = null;
                            cmiAutoWrap.Icon = img;
                            cmiNoWrap.Icon = null;
                            break;
                        }
                    case AutoWrapType.No:
                        {
                            cmiWrapFromDoc.Icon = null;
                            cmiAutoWrap.Icon = null;
                            cmiNoWrap.Icon = img;
                            break;
                        }
                }
            }
        }

        static void cmiNotTask_Click(object sender, RoutedEventArgs e)
        {
            Command.SetTaskItemType(Expressions.BaseText.TaskType.NotTask);
        }

        static void cmiPauseTask_Click(object sender, RoutedEventArgs e)
        {
            Command.SetTaskItemType(Expressions.BaseText.TaskType.Paused);
        }

        static void cmiAbortTask_Click(object sender, RoutedEventArgs e)
        {
            Command.SetTaskItemType(Expressions.BaseText.TaskType.Aborted);
        }

        static void cmiFinishedTask_Click(object sender, RoutedEventArgs e)
        {
            Command.SetTaskItemType(Expressions.BaseText.TaskType.Finished);
        }

        static void cmiRunningTask_Click(object sender, RoutedEventArgs e)
        {
            Command.SetTaskItemType(Expressions.BaseText.TaskType.Running);
        }

        static void cmiNotStartedTask_Click(object sender, RoutedEventArgs e)
        {
            Command.SetTaskItemType(Expressions.BaseText.TaskType.NotStarted);
        }

        static void cmiHVLine_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextMaskHVLine();
        }

        static void cmiVLine_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextMaskVLine();
        }

        static void cmiHLine_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextMaskHLine();
        }

        static void cmiNoneMask_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextMaskNone();
        }

        static void cmiRightBias_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextMaskRightBias();
        }

        static void cmiLeftBias_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextMaskLeftBias();
        }

        static void cmiPasteOverride_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextPasteOverride();
        }

        static void cmiSwitchShowBackText_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchShowBackText();
        }

        static void cmiPasteAdd_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextPasteAdd();
        }

        static void cmiCopyBaseText_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextCopyText();
        }

        static void cmiClearBaseText_Click(object sender, RoutedEventArgs e)
        {
            Command.BaseTextClearText();
        }

        static void cmiEditBaseText_Click(object sender, RoutedEventArgs e)
        {
            Command.SelectInOrEdit();
        }
        #endregion

        #region 扩展式快捷菜单处理器

        static void cmiExpanderTurnoff_Click(object sender, RoutedEventArgs e)
        {
            Command.TurnOffMSPanels();
        }

        static void cmiSwitchCollapse_Click(object sender, RoutedEventArgs e)
        {
            Command.ExpandAndCollapse();
        }

        static void cmiSwitchSlavePanelIndent_Click(object sender, RoutedEventArgs e)
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.Expander == false) return;

            Expressions.Expander expander = Globals.selectedEditor.SelectedExpression as Expressions.Expander;

            ModifingItem mi = new ModifingItem();
            Action action = new Action(expander.ExID, -1, expander.GetType().Name, "IsInsideIndent",
                expander.IsInsideIndent.ToString(), (!expander.IsInsideIndent).ToString());

            expander.IsInsideIndent = !expander.IsInsideIndent;//翻转从子面板缩进状态。

            mi.Add(action);
            expander.Editor.Do(mi);
        }

        static void setButtonText_Click(object sender, RoutedEventArgs e)
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.Expander == false) return;

            Expressions.Expander expander = Globals.selectedEditor.SelectedExpression as Expressions.Expander;
            expander.SetButtonText();
        }
        #endregion

        #region 图片框式快捷菜单处理器
        static void cmiSelectImageFile_Click(object sender, RoutedEventArgs e)
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is Expressions.PictureBox == false) return;

            Expressions.PictureBox pb = Globals.selectedEditor.SelectedExpression as Expressions.PictureBox;
            pb.LoadImageFromFile();
        }
        #endregion

        #region [矛盾/对立]关系式快捷菜单处理器
        static void cmiSetOpposite_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchToOpposite();
        }

        static void cmiSetContradiction_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchToContradiction();
        }
        #endregion

        #region 子面板快捷菜单处理器
        static void cmiSwitchDirection_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchDirection();
        }
        #endregion

        #region 箭头式菜单事件处理器

        static void arrowContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;

            Expressions.Arrow arrow = Globals.selectedEditor.SelectedExpression as Expressions.Arrow;
            if (arrow == null) return;

            Image img = new Image();
            img.Width = img.Height = 16;
            img.Source = new BitmapImage(new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/Selected.png"));

            switch (arrow.Arrows)
            {
                case Expressions.Arrow.ArrowLocation.All:
                    {
                        cmiArrowAll.Icon = img;
                        cmiArrowStart.Icon = null;
                        cmiArrowEnd.Icon = null;
                        cmiArrowNone.Icon = null;
                        break;
                    }
                case Expressions.Arrow.ArrowLocation.Start:
                    {
                        cmiArrowAll.Icon = null;
                        cmiArrowStart.Icon = img;
                        cmiArrowEnd.Icon = null;
                        cmiArrowNone.Icon = null;
                        break;
                    }
                case Expressions.Arrow.ArrowLocation.End:
                    {
                        cmiArrowAll.Icon = null;
                        cmiArrowStart.Icon = null;
                        cmiArrowEnd.Icon = img;
                        cmiArrowNone.Icon = null;
                        break;
                    }
                //case Expressions.Arrow.ArrowLocation.None:
                default:
                    {
                        cmiArrowAll.Icon = null;
                        cmiArrowStart.Icon = null;
                        cmiArrowEnd.Icon = null;
                        cmiArrowNone.Icon = img;
                        break;
                    }
            }
        }

        static void cmiArrowNone_Click(object sender, RoutedEventArgs e)
        {
            Command.ArrowLocationSetup(Expressions.Arrow.ArrowLocation.None);
        }

        static void cmiArrowStart_Click(object sender, RoutedEventArgs e)
        {
            Command.ArrowLocationSetup(Expressions.Arrow.ArrowLocation.Start);
        }

        static void arrowExample_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Command.ArrowLocationSetup(Expressions.Arrow.ArrowLocation.End);
        }

        static void cmiArrowAll_Click(object sender, RoutedEventArgs e)
        {
            Command.ArrowLocationSetup(Expressions.Arrow.ArrowLocation.All);
        }
        #endregion

        #region 行快捷菜单事件处理器

        /// <summary>
        /// 行的扩展与收缩。扩展：将行所在的父面板中的兄弟公式放到行中去；收缩：将行中的公式放到行所在的父面板中去。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void rowOperationControl_CmdButtonClicked(object sender, Widegets.RowOperationButtonClickedEventArgs e)
        {
            Commands.Command.RosideMove(e.Cmd);
        }

        static void rowHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("row.htm");
        }

        static void cmiRowDirection_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchDirection();
        }
        #endregion

        #region 富文本块快捷菜单事件处理器

        static void cmmiRTEdit_Click(object sender, RoutedEventArgs e)
        {
            Command.EditRichText(Globals.selectedEditor.SelectedExpression as Expressions.RichText);
        }

        static void richTextContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            if (Globals.selectedEditor == null || Globals.selectedEditor.SelectedExpression == null) return;
            if (Globals.selectedEditor.SelectedExpression is IAutoWrap)
            {
                IAutoWrap autoWrapEx = Globals.selectedEditor.SelectedExpression as IAutoWrap;

                Image img = new Image();
                img.Width = img.Height = 16;
                img.Source = new BitmapImage(new Uri("pack://Application:,,,/LunarPresentation;component/Resources/Icons/Selected.png"));

                switch (autoWrapEx.AutoWrap)
                {
                    case AutoWrapType.FromDoc:
                        {
                            cmiRTWrapFromDoc.Icon = img;
                            cmiRTAutoWrap.Icon = null;
                            cmiRTNoWrap.Icon = null;
                            break;
                        }
                    case AutoWrapType.Wrap:
                        {
                            cmiRTWrapFromDoc.Icon = null;
                            cmiRTAutoWrap.Icon = img;
                            cmiRTNoWrap.Icon = null;
                            break;
                        }
                    case AutoWrapType.No:
                        {
                            cmiRTWrapFromDoc.Icon = null;
                            cmiRTAutoWrap.Icon = null;
                            cmiRTNoWrap.Icon = img;
                            break;
                        }
                }
            }
        }

        static void richTextHeader_Click(object sender, RoutedEventArgs e)
        {
            ShowHelpFile("richText.htm");
        }

        static void cmiRTAutoWrap_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchExAutoWrap(AutoWrapType.Wrap);
        }

        static void cmiRTNoWrap_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchExAutoWrap(AutoWrapType.No);
        }

        static void cmiRTWrapFromDoc_Click(object sender, RoutedEventArgs e)
        {
            Command.SwitchExAutoWrap(AutoWrapType.FromDoc);
        }

        #endregion

        #region 需要单独控制的三个“文本块自动折行子菜单项”。
        public static MenuItem cmiAutoWrap;

        public static MenuItem cmiNoWrap;

        public static MenuItem cmiWrapFromDoc;
        #endregion

        #region 需要单独控制的三个“富文本块自动折行子菜单项”。
        public static MenuItem cmiRTAutoWrap;

        public static MenuItem cmiRTNoWrap;

        public static MenuItem cmiRTWrapFromDoc;
        #endregion

        public static ContextMenu arrowContextMenu;

        public static MenuItem cmiArrowStart;
        public static MenuItem cmiArrowNone;
        public static MenuItem cmiArrowAll;
        public static MenuItem cmiArrowEnd;

        public static ContextMenu braceContextMenu;

        public static ContextMenu subpanelContextMenu;

        public static ContextMenu gridPanelContextMenu;

        public static ContextMenu leafPanelContextMenu;

        public static ContextMenu contradictionContextMenu;

        public static ContextMenu pictureBoxContextMenu;

        public static ContextMenu expanderContextMenu;

        public static ContextMenu baseTextContextMenu;

        public static ContextMenu bracketContextMenu;

        public static ContextMenu triangleContextMenu;

        public static ContextMenu markTextContextMenu;

        public static ContextMenu genuspaciesContextMenu;

        public static ContextMenu crossContextMenu;

        public static ContextMenu gridContextMenu;

        public static ContextMenu treeContextMenu;

        public static ContextMenu rowContextMenu;

        public static ContextMenu richTextContextMenu;

        internal static void OpenMenu(Editor editor)
        {
            if (editor == null || editor.SelectedExpression == null) return;

            if (editor.SelectedExpression is Expressions.Arrow)
            {
                ExContextMenus.arrowContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.Brace)
            {
                ExContextMenus.braceContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.GridPanel)
            {
                //注意顺序：GridPanel是SubPanel的子类。
                ExContextMenus.gridPanelContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.LeafPanel)
            {
                //与GridPanel类似，这个也要注意顺序——它也是SubPanel的子类。
                ExContextMenus.leafPanelContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.SubPanel)
            {
                ExContextMenus.subpanelContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.LogicContradiction)
            {
                ExContextMenus.contradictionContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.PictureBox)
            {
                ExContextMenus.pictureBoxContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.Expander)
            {
                ExContextMenus.expanderContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.BaseText)
            {
                ExContextMenus.baseTextContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.Bracket)
            {
                ExContextMenus.bracketContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.Triangle)
            {
                ExContextMenus.triangleContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.MarkText)
            {
                ExContextMenus.markTextContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.LogicGenuspecies)
            {
                ExContextMenus.genuspaciesContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.LogicCross)
            {
                ExContextMenus.crossContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.Grid)
            {
                ExContextMenus.gridContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.Row)
            {
                ExContextMenus.rowContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.RichText)
            {
                ExContextMenus.richTextContextMenu.IsOpen = true;
            }
            else if (editor.SelectedExpression is Expressions.Tree)
            {
                ExContextMenus.treeContextMenu.IsOpen = true;
            }
        }
    }
}
